package main.katalog;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import main.katalog.model.item;

/**
 * Servlet implementation class items
 */
@WebServlet("/items")
public class items extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public items() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @throws IOException 
	 * @throws ServletException 
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
    public void doGetPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    	response.setContentType("text/html;charset=UTF-8");
		request.setCharacterEncoding("utf-8");
		
		String action = request.getParameter("action");
		HttpSession session = request.getSession();
		
		if(action.equals("add")){
			String type = request.getParameter("type");
			String name = request.getParameter("typeName");
			String dirName = request.getParameter("yname");
			String desc = request.getParameter("desc");
			if(name.equals("") || dirName.equals("")){
				session.setAttribute("bos", true);
				request.getRequestDispatcher("/add.jsp").forward(request,response);
				return;
			}
			session.setAttribute("bos", false);
			int userID = (int) session.getAttribute("userID");
			item item = new item();
			try {
				boolean bool = item.addItem(name, dirName, userID, desc, type);
				if(!bool){
					session.setAttribute("invalidAdd", true);
					session.setAttribute("itemType", type);
					request.getRequestDispatcher("/add.jsp").forward(request,response);
				}else{
					session.setAttribute("invalidAdd", false);
			//		request.getRequestDispatcher("/welcome.jsp").forward(request,response);
					response.sendRedirect("/katalog/items?action=listAll&id="+userID);
				}

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		else if (action.equals("delete")){
			int itemID = Integer.parseInt(request.getParameter("id"));
			item it = new item();
			it.getItem(itemID);
			int userID =  (int) session.getAttribute("userID");
			if(it.equals(null)){
				session.setAttribute("delete", "invalid");
			}
			else{
				session.setAttribute("delete", "valid");
				if(it.getOwnerID()==userID){
					it.deleteItem();	
					session.setAttribute("validDelete", "valid");
				}
				else
					session.setAttribute("validDelete", "invalid");
			}
			request.getRequestDispatcher("items?id="+userID+"&action=listAll").forward(request,response);	
		}
		else if (action.equals("list")){
			
		}
		else if ( action.equals("listAll") ){
			int userID=0;
			if(request.getParameter("id")!=null){
				userID = Integer.parseInt(request.getParameter("id"));
			}
			else{
				userID = Integer.parseInt(request.getParameter("id"));
			}
			item it = new item();
			List<item> list = new ArrayList<item>();
			list = it.listItem(userID);
			request.setAttribute("mycatalog", it.listItem(userID));
			request.getRequestDispatcher("/myCatalog.jsp").forward(request,response);	
		
		}
		else if ( action.equals("view") ){
			item it = new item();
			int itemID = Integer.parseInt(request.getParameter("itemID"));
			it.getItem(itemID);
			request.setAttribute("item", it);
			request.getRequestDispatcher("/view.jsp").forward(request, response);
		}
		else {
			
		}
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGetPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGetPost(request, response);
		
	}


}
